package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.enums.forattitude.TypeEnumForAttitude;
import com.jilefojing.beans.pojos.Attitude;
import com.jilefojing.beans.query.AttitudeQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IAttitudeDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IAttitudeMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月22日 01时02分09秒
 */
@Repository
public class AttitudeDao extends BaseDao<Attitude> implements IAttitudeDao {
	@Autowired
	private IAttitudeMapper attitudeMapper;

	@Override
	public Pagination<Attitude> getPagination(AttitudeQuery attitudeQuery, Pagination<Attitude> pagination) {
		Example example = new Example(Attitude.class);
		Criteria criteria = example.createCriteria();
		if (Detect.notEmpty(attitudeQuery.getIds())) {
			criteria.andIn(Attitude.FIELD_ID, attitudeQuery.getIds());
		}

		if (Detect.notEmpty(attitudeQuery.getTypes())) {
			List<Short> typeCodes = new ArrayList<Short>();
			for (TypeEnumForAttitude _type : attitudeQuery.getTypes()) {
				typeCodes.add(_type.getCode());
			}
			criteria.andIn(Attitude.FIELD_TYPE, typeCodes);
		}

		if (Detect.notEmpty(attitudeQuery.getUserIds())) {
			criteria.andIn(Attitude.FIELD_USERID, attitudeQuery.getUserIds());
		}

		if (Detect.notEmpty(attitudeQuery.getTypes())) {
			List<Short> itemTypeCodes = new ArrayList<Short>();
			for (ItemTypeEnumForSource _type : attitudeQuery.getItemTypes()) {
				itemTypeCodes.add(_type.getCode());
			}
			criteria.andIn(Attitude.FIELD_ITEMTYPE, itemTypeCodes);
		}

		if (Detect.notEmpty(attitudeQuery.getItemIds())) {
			criteria.andIn(Attitude.FIELD_ITEMID, attitudeQuery.getItemIds());
		}

		if (attitudeQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(Attitude.FIELD_CREATETIME, attitudeQuery.getEqualCreateTime());
		}

		if (attitudeQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(Attitude.FIELD_CREATETIME, attitudeQuery.getEqualAndBeforCreateTime());
		}

		if (attitudeQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(Attitude.FIELD_CREATETIME, attitudeQuery.getEqualAndAfterCreateTime());
		}

		if (attitudeQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(Attitude.FIELD_CREATETIME, attitudeQuery.getBeforCreateTime());
		}

		if (attitudeQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(Attitude.FIELD_CREATETIME, attitudeQuery.getAfterCreateTime());
		}

		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}
		return super.getPagination(pagination, example);
	}
}
